package com.xiongkw.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.Date;

public class MongoService {

    public static void main(String[] args) {
        Integer pageNumber = 1;
        Integer pageSize = 30;
        MongoConfig mongoConfig = new MongoConfig();
        MongoDatabase mongoDatabase = mongoConfig.getMongoDatabase();
        MongoCollection<Document> collection = mongoDatabase.getCollection("user_feature");


        final long start = System.currentTimeMillis();
        for(int i = 0; i<100; i++){
            MongoCursor<Document> iterator = collection.find(new BasicDBObject("userId", 1001)).sort(new BasicDBObject("_id",1)).skip((pageNumber-1)*pageSize).limit(pageSize).iterator();
            Integer count = 0;
            while (iterator.hasNext()){
                while (iterator.hasNext()){
                    Document next = iterator.next();
                    String id = next.getObjectId("_id").toString();
                    String userName = next.getString("userName");
                    Object favorateColors = next.get("favorateColor");
                    count ++;
                    System.out.println(id);
                }
                pageNumber ++ ;
    //            iterator = collection.find(new BasicDBObject("application_type", 2)).sort(new BasicDBObject("_id",1)).skip((pageNumber-1)*pageSize).limit(pageSize).iterator();
            }
        }


        final long end = System.currentTimeMillis();

        System.out.println(end-start);

    }
}
